/**
 * 
 */
package com.ccqlk.attendance.api.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ccqlk.attendance.api.APIBaseController;
import com.ccqlk.attendance.model.OnlineUser;
import com.ccqlk.attendance.model.User;
import com.ccqlk.attendance.repository.service.OnlineUserService;
import com.ccqlk.attendance.repository.service.UserService;
import com.ccqlk.utils.RequestUtil;
import com.google.common.collect.Maps;

/**
 * Copyright 2015-2018 重庆鲁控科技有限公司<br>
 * 文件标题: APILoginController<br>
 * 文件说明: 登录页面<br>
 * @author ZhangYinxian 348893009@qq.com<br>
 * 创建时间 2016年5月30日 下午5:30:48<br>
 */
@RestController
@RequestMapping(APIBaseController.API_VERSION)
public class APILoginController extends APIBaseController {

	private @Resource UserService userService;
	private @Resource OnlineUserService onlineUserService;
	
	@RequestMapping("login")
	public Model login(HttpServletRequest request,HttpServletResponse response, Model model){
		String username = RequestUtil.getValueByName(request, "username");
		String password = RequestUtil.getValueByName(request, "password");
		
		if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
			return renderFail(request, response, model, "用户名或密码错误");
		}
		
		Map<String, Object> params = Maps.newHashMap();
		params.put("username", username);
		
		Md5Hash md5Hash = new Md5Hash(password);
		password = md5Hash.toString();
		params.put("password", password);
		
		List<User> users = userService.selectByParams(params);
		if (users==null || users.isEmpty()) {
			return renderFail(request, response, model, "用户名或密码错误");
		}
		
		User user = users.get(0);
		
		OnlineUser onlineUser = new OnlineUser();
		String token = UUID.randomUUID().toString();
		onlineUser.setToken(token);
		onlineUser.setUserCode(user.getCode());
		onlineUser.setAddtime(new Date());
		onlineUserService.add(onlineUser);
		
		model.addAttribute("user", user);
		model.addAttribute("token", token);
		return renderSuccess(request, response, model, "登录成功");
	}
	
	@RequestMapping("logout")
	public Model logout(HttpServletRequest request, HttpServletResponse response, Model model){
		String token = RequestUtil.getValueByName(request, "token");
		if (StringUtils.isEmpty(token)) {
			return renderFail(request, response, model, "token错误");
		}
		
		List<OnlineUser> onlineUsers = onlineUserService.selectByToken(token);
		if (onlineUsers==null || onlineUsers.isEmpty()) {
			return renderSuccess(request, response, model);
		}
		
		for (OnlineUser onlineUser : onlineUsers) {
			onlineUserService.delete(onlineUser.getId());
		}
		return renderSuccess(request, response, model);
	}
}
